package com.dwjm.shop.goods;

import com.dwjm.shop.utils.ConnectionDatabaseForMySQLUseJDBC;

import javax.xml.transform.Result;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class GoodDaoImpl implements GoodDao {
    Connection connection = null;
    PreparedStatement psmt = null;
    ResultSet rset = null;

    public GoodDaoImpl() {
        connection = ConnectionDatabaseForMySQLUseJDBC.getConnection();
    }


    @Override
    public int addGoods(Goods goods) {
        try {
            psmt = connection.prepareStatement("insert into t_goods(title,price,description,img,state,mid) values(?,?,?,?,?,?)");
            psmt.setString(1, goods.getTitle());
            psmt.setBigDecimal(2, goods.getPrice());
            psmt.setString(3, goods.getDescription());
            psmt.setString(4, goods.getImg());
            psmt.setInt(5, goods.getState());
            psmt.setInt(6, goods.getMid());
            int i = psmt.executeUpdate();
            return i == 1 ? 1 : 0;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public List<Goods> findAllGoods(Integer page, Integer size) {
        List<Goods> list = new ArrayList<>();
        try {
            psmt = connection.prepareStatement("select * from t_goods limit ?,?");
            psmt.setInt(1, (page - 1) * size);
            psmt.setInt(2, size);
            rset = psmt.executeQuery();
            Goods goods1 = null;
            while (rset.next()) {
                goods1 = new Goods(rset.getInt("id"),
                        rset.getString("title"),
                        rset.getBigDecimal("price"),
                        rset.getString("description"),
                        rset.getString("img"),
                        rset.getInt("state"),
                        rset.getInt("mid")
                );
                list.add(goods1);
            }
            return list;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;//只要出错就是空
}

    @Override
    public int countGoods() {
        int total = 0;
        try {
            psmt = connection.prepareStatement("select count(*) from t_goods");
            rset = psmt.executeQuery();
            if (rset.next()) {
                total = rset.getInt(1);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return total;
    }

    @Override
    public List<Goods> findGoodsByKey(String key) {
        List<Goods> list = new ArrayList<>();
        try {
            psmt = connection.prepareStatement("select * from t_goods where title like ?");
            psmt.setString(1, "%" + key + "%");
            rset = psmt.executeQuery();
            while (rset.next()){
                Goods goods = new Goods(rset.getInt("id"),
                        rset.getString("title"),
                        rset.getBigDecimal("price"),
                        rset.getString("description"),
                        rset.getString("img"),
                        rset.getInt("state"),
                        rset.getInt("mid"));
                        list.add(goods);
            }
            return list;
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }
    }

    public int deleteGoods(Integer id) {
        try {
            psmt = connection.prepareStatement("delete from t_goods where id = ?");
            psmt.setInt(1, id);
            int i = psmt.executeUpdate();
            return i == 1 ? 1 : 0;
        } catch (SQLException e) {
            e.printStackTrace();
            return 0;
        }
    }
}
